package vn.anhqn.englishsentensetranslator.activity;

import vn.anhqn.englishsentensetranslator.R;
import vn.anhqn.englishsentensetranslator.SentenseLookup.SentenseListFragment;
import vn.anhqn.englishsentensetranslator.addsentense.AddSentenseFragment;
import vn.anhqn.englishsentensetranslator.base.activity.BaseFragmentActivity;
import vn.anhqn.englishsentensetranslator.constant.ConfigApp;
import vn.anhqn.englishsentensetranslator.constant.Constant;
import vn.anhqn.englishsentensetranslator.onlinetranslate.OnlineTranslateFragment;
import vn.anhqn.englishsentensetranslator.selectlanguage.SelectLanguageFragment;
import vn.anhqn.englishsentensetranslator.utils.PreferenceHelper;
import vn.anhqn.englishsentensetranslator.utils.Utils;
import vn.anhqn.englishsentensetranslator.widget.HeverticalTextView;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu.OnOpenListener;

public class MainActivity extends BaseFragmentActivity implements OnClickListener, OnOpenListener {
	private ImageView imvLeftHeaderIcon;
	private SlidingMenu menu;
	private HeverticalTextView tvTitle;
	private HeverticalTextView tvYourLangSelected;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initSlidingMenu();
		initView();
		if (!existsFragment()) {
			if (PreferenceHelper.getInstance(this).getInt(ConfigApp.PREFERENCES_LANGUAGE_INDEX, -1) == -1)
				addFirstFragment(SelectLanguageFragment.class);
			else {
				addFirstFragment(OnlineTranslateFragment.class);
			}
		}

	}

	private void initSlidingMenu() {
		menu = new SlidingMenu(this);
		menu.setMode(SlidingMenu.LEFT);
		menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		menu.setFadeDegree(0.35f);
		menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
		menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		menu.setShadowWidthRes(R.dimen.slidingmenu_shadow_width);
		menu.setShadowDrawable(R.drawable.sliding_menu_shadow);
		menu.setMenu(R.layout.sliding_menu);
		ViewGroup rootMenu = (ViewGroup) menu.findViewById(R.id.rl_root);
		for (int i = 0; i < rootMenu.getChildCount(); i++) {
			View v = rootMenu.getChildAt(i);
			if (v instanceof HeverticalTextView)
				v.setOnClickListener(this);
		}
		menu.setOnOpenListener(this);
	}

	public void enableSlidingMenu(boolean isEnable) {
		menu.setSlidingEnabled(isEnable);
	}

	private void initView() {
		imvLeftHeaderIcon = (ImageView) findViewById(R.id.imv_left_header);
		tvTitle = (HeverticalTextView) findViewById(R.id.tv_header_title);
		tvYourLangSelected = (HeverticalTextView) findViewById(R.id.tv_menu_your_language_selected);
		setTextLangSelected();
	}

	public void setTextLangSelected() {
		int indexLang = PreferenceHelper.getInstance(this).getInt(ConfigApp.PREFERENCES_LANGUAGE_INDEX, 0);
		String[] arrLanguages = getResources().getStringArray(R.array.language_arr);
		tvYourLangSelected.setText(arrLanguages[indexLang]);
	}

	public ImageView getLeftHeaderIcon() {
		return imvLeftHeaderIcon;
	}

	public void setLeftHeaderIcon(int resId) {
		this.imvLeftHeaderIcon.setImageResource(resId);
	}

	public void setHeaderTitle(String title) {
		tvTitle.setText(title);
	}

	public SlidingMenu getSlidingMenu() {
		return menu;
	}

	@Override
	public void onClick(View v) {
		int idView = v.getId();
		switch (idView) {
		case R.id.tv_menu_sentense_lookup:
			switchFragment(SentenseListFragment.class);
			menu.toggle();
			break;
		case R.id.tv_menu_add_new_sentense:
			replaceNewInstance(AddSentenseFragment.class);
			menu.toggle();
			break;
		case R.id.tv_menu_translate:
			switchFragment(OnlineTranslateFragment.class);
			menu.toggle();
			break;
		case R.id.tv_menu_your_language:
			menu.toggle();
			switchFragment(SelectLanguageFragment.class);
			break;
		case R.id.tv_menu_about_us:

			break;

		default:
			break;
		}
	}

	@Override
	public void onOpen() {
		Utils.hideSoftKeyboard(this);
	}
}
